package com.weiwei.diancan.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.weiwei.diancan.R;
import com.weiwei.diancan.bean.LoginBean;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by BMW on 2016/11/7.
 */
public class MainDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {
    private List<LoginBean.TypeListBean> categoryList;
    private List<LoginBean.TypeListBean.TabListBean> teamList = new ArrayList<>();
    private Context mContext;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public MainDetailAdapter(Context context, List<LoginBean.TypeListBean> categoryList) {
        mContext = context;
        setCategoryList(categoryList);
    }

    public void setCategoryList(List<LoginBean.TypeListBean> categoryList) {
        this.categoryList = categoryList;
        teamList.clear();
        for(int i = 0;i<categoryList.size();i++){
            if(teamList!=null){
                teamList.addAll(categoryList.get(i).getTab_list());
            }
        }
        notifyDataSetChanged();
    }



    public List<LoginBean.TypeListBean> getCategoryList() {
        return categoryList;
    }

    @Override
    public int getItemCount() {
        return teamList.size();
    }

    /**
     * 返回值相同会被默认为同一项
     * @param position
     * @return
     */
    @Override
    public long getHeaderId(int position) {
        return getSortType(position);
    }

    //获取当前球队的类型
    public int getSortType(int position) {
        int sort = -1;
        int sum = 0;
        for (int i=0;i<categoryList.size();i++){
            if(position>=sum){
                sort++;
            }else {
                return sort;
            }
            sum += categoryList.get(i).getTab_list().size();
        }
        return sort;
    }

    /**
     * ===================================================================================================
     * header的ViewHolder
     * ===================================================================================================
     */
    @Override
    public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_main_detail_group, viewGroup, false);
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        TextView textView = (TextView) viewHolder.itemView.findViewById(R.id.tv_fenlei);
        textView.setText(categoryList.get(getSortType(position)).getType_name());
        //textView.setBackgroundColor(getRandomColor());
    }


    private class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }

//    private int getRandomColor() {
//        SecureRandom rgen = new SecureRandom();
//        return Color.HSVToColor(150, new float[]{
//                rgen.nextInt(359), 1, 1
//        });
//    }

    /**
     * ==================================================================================================
     * 以下为contentViewHolder
     * ==================================================================================================
     */
    @Override
    public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_main_detail_child, parent, false);
        return new ContentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        holder.itemView.setOnClickListener(v -> {
            if(onItemClickListener!=null){
                onItemClickListener.onItemClick(holder.itemView,position,teamList.get(position));
            }
        });

        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        LoginBean.TypeListBean.TabListBean tabListBean = teamList.get(position);
        viewHolder.tv_table_name.setText(tabListBean.getTab_name());
        if(tabListBean.getTab_state().equals("1")||tabListBean.getTab_state().equals("0")){
            viewHolder.tv_null.setVisibility(View.VISIBLE);

            if(tabListBean.getTab_state().equals("1")){
                viewHolder.tv_null.setText("空闲");
            }else{
                viewHolder.tv_null.setText("暂停");
            }

            viewHolder.tv_price.setVisibility(View.GONE);
            viewHolder.tv_table_name.setTextColor(Color.parseColor("#181818"));
            viewHolder.tv_people_num.setText("0/"+tabListBean.getTab_person());
            viewHolder.tv_people_num.setTextColor(Color.parseColor("#181818"));
        }else {
            viewHolder.tv_null.setVisibility(View.GONE);
            viewHolder.tv_price.setVisibility(View.VISIBLE);
            viewHolder.tv_price.setText("¥"+tabListBean.getTab_price());
            viewHolder.tv_table_name.setTextColor(Color.parseColor("#B41B1B"));

            if(tabListBean.getOrder_list()==null||tabListBean.getOrder_list().size()==0){
                viewHolder.tv_people_num.setText("0/"+tabListBean.getTab_person());
            }else {
                viewHolder.tv_people_num.setText(tabListBean.getOrder_list().get(0).getOrder_person() + "/" + tabListBean.getTab_person());
            }
            viewHolder.tv_people_num.setTextColor(Color.parseColor("#B41B1B"));
        }
    }

    private class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView tv_table_name;
        TextView tv_people_num;
        TextView tv_null;
        TextView tv_price;
        public ContentViewHolder(View itemView) {
            super(itemView);
            tv_table_name = (TextView) itemView.findViewById(R.id.tv_table_name);
            tv_people_num = (TextView) itemView.findViewById(R.id.tv_people_num);
            tv_null = (TextView) itemView.findViewById(R.id.tv_null);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(View itemView, int position, LoginBean.TypeListBean.TabListBean tabListBean);
    }
}
